package com.example.costs;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import base.Base;
import base.DB;
import base.Lg;
import base.Transaction;

public class AddTransActivity extends Activity implements TextWatcher {
    private EditText editText_price;
    private AutoCompleteTextView aCTxtVewCateg;
    private AutoCompleteTextView aCTxtVewDesr;
    private EditText editTextDate;
    private String trType;
    private int DIALOG_DATE = 1;
    private Cursor cursor;
    private int uId;
    private DB db;
    Calendar cal;
    private ArrayList arrayCtg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_trans);

        db = new DB(this);
        db.open();

        aCTxtVewCateg = (AutoCompleteTextView) findViewById(R.id.aCTxtVewCateg);
        aCTxtVewDesr = (AutoCompleteTextView) findViewById(R.id.aCTxtVewDesr);
        editText_price = (EditText) findViewById(R.id.editText_price);
        editTextDate = (EditText) findViewById(R.id.editTextDate);
        trType = MainActivity.trType;
        uId = 1;

        fillCtgData();
        aCTxtVewCateg.addTextChangedListener(this);
        aCTxtVewCateg.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, arrayCtg));

    }

    public void onClickBtAddTrans(View view){
        String date = editTextDate.getText().toString();
        if ("".equals(date)){
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MMM.yyyy", Locale.ENGLISH);
            date = dateFormat.format(new Date());
        }
        String amount = editText_price.getText().toString();
        String ctgNme = aCTxtVewCateg.getText().toString();
        String descriprion = aCTxtVewDesr.getText().toString();
        Log.i("onClickBtAddTrans",date);
        Transaction tr = new Transaction(uId,date,amount,null,ctgNme,trType,descriprion);
        tr.toBase();
        Lg.d("Transaction - " + tr.toString());
        finish();
    }

    public void onTextChanged(
            CharSequence s, int start, int before, int count) {
//        mText.setText(mAutoComplete.getText());
    }

    public void beforeTextChanged(
            CharSequence s, int start, int count, int after) {
    }

    public void afterTextChanged(Editable s) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.add_trans, menu);
        return true;
    }

    public void onClickDateText(View view) {
        showDialog(DIALOG_DATE);
    }


    protected Dialog onCreateDialog(int id) {
        if (id == DIALOG_DATE) {
            cal = Calendar.getInstance();
            DatePickerDialog tpd = new DatePickerDialog(this, myCallBack,cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
            return tpd;
        }
        return super.onCreateDialog(id);
    }

    DatePickerDialog.OnDateSetListener myCallBack = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            editTextDate.setText(Base.makeDate(dayOfMonth, monthOfYear, year));
        }
    };

    private void fillCtgData() {
        cursor = db.fetchAllCtg(uId, trType);
        arrayCtg = new ArrayList();
        if (cursor != null) {
            while(cursor.moveToNext()) {
                arrayCtg.add(cursor.getString(0));
            }
            cursor.close();
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }


}
